<?php

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Validation\ValidationException;
use App\Http\Middleware\JWTAuthMiddleware;
use App\Http\Middleware\LogMiddleware;


return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__ . '/../routes/web.php',
        commands: __DIR__ . '/../routes/console.php',
        channels: __DIR__ . '/../routes/channels.php', // 加载 socket 路由模块
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware): void {
        $middleware->validateCsrfTokens(except: [
            '*',
        ]);

        $middleware->alias([
            // 注册中间件别名
            'jwt_auth' => JWTAuthMiddleware::class,
            'log'      => LogMiddleware::class
        ]);

    })
    ->withExceptions(function (Exceptions $exceptions): void {

        // 自定义错误信息
        $exceptions->render(function (ValidationException $e) {
            return response([
                'code'   => 'B0001',
                'msg'    => $e->getMessage(),
                'errors' => $e->errors()
            ], $e->status);
        });
    })
    ->create();
